package com.huyna.rsspaper.parser;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.huyna.rsspaper.model.Article;

public class RssParser extends DefaultHandler{
	private Article mCurrentArticle = new Article();
	private List<Article> mArticleList = new ArrayList<Article>();
	
	private int mArticleAdd = 0;
	private static int ARTICLE_LIMIT = 10;
	private boolean bTitle;
	private boolean bLink;
	private boolean bDescription;
	private boolean bPubDate;
	private boolean bImage;
	
	StringBuffer chars = new StringBuffer();
	
	public List<Article> getArticleList(){
		return this.mArticleList;
	}
	
	public void startElement(String uri, String localName, String qName, Attributes atts) {
		//chars = new StringBuffer();
        if ("item".equals(qName)) {
        	mCurrentArticle = new Article();
        } else if ("title".equals(qName)) {
        	bTitle = true;
        } else if ("link".equals(qName)) {
            bLink = true;
        } else if("description".equals(qName)){
        	bDescription = true;
        } else if("pubDate".equals(qName)){
        	bPubDate = true;
        } else if("image".equals(qName)){
        	bImage = true;
        }
	}
	
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if ("item".equals(qName)) {
			mArticleList.add(mCurrentArticle);
			mCurrentArticle = null;
        } else if ("title".equals(qName)) {
            bTitle = false;
        } else if ("link".equals(qName)) {
            bLink = false;
        } else if("description".equals(qName)){
        	bDescription = false;
        } else if("pubDate".equals(qName)){
        	bPubDate = false;
        } else if("image".equals(qName)){
        	bImage = false;
        }
	}
	
	public void characters(char ch[], int start, int length) {
		//chars.append(new String(ch, start, length));
		if (bTitle) {
            if (mCurrentArticle != null){
            	mCurrentArticle.setTitle(new String(ch, start, length));
            	bTitle = false;
            }
        } else if (bLink) {
            if (mCurrentArticle != null) {
            	try{
            		mCurrentArticle.setArticleURL(new URL(new String(ch, start, length)));
            	} catch(MalformedURLException e){
    				Log.e("URL image error", e.getMessage());
       			}
            	bLink = false;
            }
        } else if(bDescription){
        	if(mCurrentArticle != null){
        		mCurrentArticle.setDescription(new String(ch, start, length));
        		bDescription = false;
        	}
        } else if(bPubDate){
        	if(mCurrentArticle != null){
        		mCurrentArticle.setPubDate(new String(ch, start, length));
        		bPubDate = false;
        	}
        } else if(bImage){
        	if(mCurrentArticle != null){
        		try{
        			mCurrentArticle.setImageURL(new URL(new String(ch, start, length)));
        		} catch(MalformedURLException e){
    				Log.e("URL image error", e.getMessage());
       			}
        		bImage = false;
        	}
        }
	}
}
